import baseUrl from '@/utils/baseUrl.js' // api接口地址
import {
  getUrlParam
} from '@/utils/common-api.js'
export default {
  data() {
    return {
      list: [
        '张**办理成功',
        '李**办理成功',
        '绿**办理成功',
        '赵**办理成功',
        '白**办理成功',
        '罗**办理成功',
        '赵**办理成功',
        '周**办理成功',
        '王**办理成功',
        '罗**办理成功',
        '雷**办理成功',
        '米**办理成功',
        '卞**办理成功',
        '项**办理成功',
        '解**办理成功',
        '裴**办理成功',
        '温**办理成功',
        '蒲**办理成功',
        '丁**办理成功',
        '邓**办理成功',
        '洪**办理成功',
        '宗**办理成功',
        '樊**办理成功',
        '平**办理成功',
        '伍**办理成功',
        '时**办理成功',
        '乌**办理成功',
        '艾**办理成功',
        '广**办理成功',
        '于**办理成功',
        '梁**办理成功',
        '巫**办理成功',
        '轩**办理成功',
        '上**办理成功',
        '孔**办理成功',
        '曹**办理成功',
        '金**办理成功',
        '毕**办理成功',
        '计**办理成功',
        '贝**办理成功',
        '汪**办理成功',
        '安**办理成功',
        '元**办理成功',
        '薛**办理成功',
        '任**办理成功',
        '马**办理成功',
        '鲍**办理成功',
        '方**办理成功',
        '苗**办理成功',
        '水**办理成功',
        '施**办理成功',
        '郑**办理成功',
      ],
      show: false,
      content: '领取净水器需缴纳70元',
      titlestyle: {
        'color': '#333333',
        'font-size': '34rpx',
        'font-weight': 'bold',
      },
      cardOne: {
        normal_Price: 0,
        sale_Price: 0
      },
      cardTwo: {
        normal_Price: 0,
        sale_Price: 0
      },
      siteId: '', //商户端传来的店铺id
      valuenum: 6587,
      id: '', //商户端传来的id
      carSmoney: 0, //价格
      meid: '',
      showmodalts: false, //返回首页提示弹框
      carwashcoupons: false, //专项洗车券领取-只针对从商户端二维码识别进入小程序的用户弹出-丁
      isShow: false, //温馨提示--丁
      showmodal: false, //您确定要退出吗--丁
      cardcoupon: false, //领取卡券成功--丁
      gongxini: false, //已经领取过了的弹框--丁
      yesnoreceive: false, //接收是否领取卡券--丁
      carlistselfservice: [] //获取赠送的接口详情--丁
    }
  },
  onLoad(option) {
    let token = uni.getStorageSync('token')
    let resObj = null
    if (option.isCarWashStationShare || option.siteId) {
      resObj = option
    } else if (option.q) {
      resObj = getUrlParam(decodeURIComponent(option.q))
    }
    if (resObj) {
      this.siteId = resObj.siteId
      if (resObj.isCarWashStationShare) {
        this.GetRandAccountByStoreId(resObj)
        uni.setStorageSync('busstrue', true)
        uni.removeStorageSync('showmoButton')
      } else {
        this.id = resObj.id
        uni.setStorageSync('bussinessid', this.id)
        uni.setStorageSync('busstrue', true)
        uni.removeStorageSync('showmoButton')
        if (token) {
          // 登录了-扫码进入小程序落地页-就调用这个接口
          let userx = uni.getStorageSync('Ztc_C_USER_INFO')
          this.meid = userx.id
          this.SetBShare()
        }
      }
    }
    // pages_commodity/selfWash/carwash_globalche/carwash_globalche
    // uni.setStorageSync('bussinessid', '4eaf64d02fad42e9905b83ab76b547e9')
    // uni.setStorageSync('busstrue', true)
    let that = this
    uni.getSystemInfo({
      success(res) {
        that.top_height = res.statusBarHeight //data中 声明 top_height:0
      }
    })
  },
  onShow() {
    this.GetSelfServiceCarWashCardById()

    let token = uni.getStorageSync('token')
    let busstrue = uni.getStorageSync('busstrue') || ''
    if (token !== '') {
      if (busstrue) {
        //true商户端进去的用户调用弹框看是否领券
        this.CheckRegisterSelfServiceCarWashCard() //1false没有发显示弹框，true已发放不显示  2,不管是否领取都要弹
        uni.removeStorageSync('busstrue')
        return
      }
    } else {
      if (busstrue) {
        this.carwashcoupons = true
      }
    }
    uni.removeStorageSync('purchaseinformation')
  },
  computed: {
    serviceNumber() {
      return this.$store.state.serviceNumber
    }
  },
  methods: {
    //点击头部返回提示、
    navBack() {
      this.showmodalts = true
    },
    showmoClickts() {
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    async GetRandAccountByStoreId(resObj) {
      try {
        let token = uni.getStorageSync('token')
        let userInfo = uni.getStorageSync('Ztc_C_USER_INFO')
        let reqObj = {
          storeid: resObj.storeId,
          stationid: resObj.siteId,
          isLogin: token ? true : false,
        }
        // 登录的情况下把用户id传过去
        if (token && userInfo) {
          reqObj.UID = userInfo.id
        }
        const res = await this.$api.user.GetRandAccountByStoreId(reqObj)
        // 没有登录的情况下缓存起来，登录的时候传
        if (!token) {
          uni.setStorageSync('carWashStationShareId', res.data)
        }
      } catch (e) {
        console.log('e', e)
        //TODO handle the exception
      }
    },
    errorclick() {
      this.show = true
    },
    // 点击购买
    cardClick(type) {
      this.AddSelfServiceCarWashDiscountOrder(type)
    },
    //登录
    login() {
      uni.showTabBar({
        animation: true
      })
      uni.setStorage({
        key: 'launchFlag',
        data: false,
      })
      uni.navigateTo({
        url: '/pages_logins/login/login'
      })
    },
    //提交自助洗车会员卡订单
    async AddSelfServiceCarWashDiscountOrder(types) {
      var carid = null //卡片id
      var carmoney = null //价格
      if (types === 1) {
        carid = '8'
        carmoney = this.cardOne.sale_Price
      } else {
        carid = '21'
        carmoney = this.cardTwo.sale_Price
      }

      let token = uni.getStorageSync('token')
      if (!token) {
        let data = {
          self_Service_Car_Wash_Card_Id: carid,
          purchase_Channel: 3,
          self_Service_Car_Wash_Station_Id: this.siteId,
          price: carmoney
        }
        uni.setStorageSync('purchaseinformation', data)

        this.$u.debounce(this.login, 1000)
        return false
      }

      try {
        let data = {
          self_Service_Car_Wash_Card_Id: carid,
          purchase_Channel: 3, // 购买途径 1：平台 2：线下  3：推广（从落地页跳转的）
          self_Service_Car_Wash_Station_Id: this.siteId, //凤英传的商户端的门店ID
        }
        const res = await this.$api.selfServiceCarWash.AddSelfServiceCarWashDiscountOrder(data)
        uni.hideLoading()
        if (res.code === 200) {
          //res.data返回的id
          //carmoney金额
          uni.navigateTo({
            url: `/pages_commodity/newPay/newPay?orderNo=${res.data}&type=selfServiceCarWashCoupon&price=${carmoney}&machineId=${''}`
            // url: `/pages_commodity/payment/payment?orderNo=${res.data}&type=selfServiceCarWashCoupon&price=${carmoney}&machineId=${''}`
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      } catch (e) {
        console.log(e)
      }
    },

    //根据ID获取会员卡信息1
    async GetSelfServiceCarWashCardById(type, arr) {
      try {
        const res = await this.$api.commodity.GetSpreadSelfServiceCarWashCard()
        this.cardOne = res.data[0]
        this.cardTwo = res.data[1]
      } catch (e) {
        console.log(e)
      }
    },
    //数字循环--存储
    showunmber() {
      if (this.valuenum < 10000) {
        this.valuenum++
        uni.setStorage({
          key: 'digitalloop',
          data: this.valuenum
        })
        if (this.valuenum === 9999) {
          this.valuenum = 6587
        }
      }
    },
    // 获取
    panumber() {
      const value = uni.getStorageSync('digitalloop')
      this.valuenum = value
    },

    //设置B端用户邀请记录
    async SetBShare() {
      try {
        let data = {
          inviteUserId: this.id, //邀请人ID
          currentUserId: this.meid //当前人ID
        }
        const res = await this.$api.selfServiceCarWash.SetBShare(data)
        console.log('res', res)
      } catch (e) {
        console.log(e)
      }
    },
    //专项洗车券关闭-丁
    carwashcouponsClick() {
      this.showmodal = true
    },
    // 您确定要退出吗？该优惠退出后将不能找回！ 是
    showmoClick(data) {
      this.carwashcoupons = false
      uni.setStorageSync('showmoButton', true)
    },
    // 领券免费洗车-----------1弹框，不管是否领取都要弹出

    CouponfreeClick() {
      let token = uni.getStorageSync('token')
      if (token !== '') {
        this.GetMySendCard() //获取赠送的接口详情
        //false没有发，true已发放
        if (this.yesnoreceive) {
          this.carwashcoupons = false
          this.gongxini = true //-----------2已经领取了弹框
        } else {
          this.SendRegisterSelfServiceCarWashCard() //-----2未领取
        }
      } else {
        uni.navigateTo({
          url: '/pages_logins/login/login'
        })
      }

    },
    IKnow() {
      // 恭喜你已经领取了
      this.gongxini = false
    },
    //去查看
    qxcarCancel() {
      //cardid自助洗车优惠圈的id
      //id:会员卡的id
      //purchase购买途径
      let dataid = this.carlistselfservice.id
      let dataself_Service_Car_Wash_Card_Id = this.carlistselfservice.self_Service_Car_Wash_Card_Id
      let datapurchase_Channel = this.carlistselfservice.purchase_Channel
      uni.navigateTo({
        url: `/pages_userModule/myCoupon/autoWashDetails?id=${dataid}&cardId=${dataself_Service_Car_Wash_Card_Id}&purchase=${datapurchase_Channel}`
      })
      this.cardcoupon = false
    },
    //是否领取卡券
    async CheckRegisterSelfServiceCarWashCard(types) {
      const res = await this.$api.selfServiceCarWash.CheckRegisterSelfServiceCarWashCard()
      this.yesnoreceive = res.data //接收是否领取卡券
      this.carwashcoupons = true //1弹框，不管是否领取都要弹出
    },
    async SendRegisterSelfServiceCarWashCard(types) {
      const res = await this.$api.selfServiceCarWash.SendRegisterSelfServiceCarWashCard()
      this.carwashcoupons = false
      this.cardcoupon = true //领取成功----------3
    },
    //获取赠送的接口详情
    async GetMySendCard() {
      const res = await this.$api.selfServiceCarWash.GetMySendCard()
      this.carlistselfservice = res.data
    },
    //领取成功---确定
    carcancelClick() {
      this.cardcoupon = false
      this.isShow = true //温馨提示 -----------4
    },
    //温馨提示弹框
    onScanClick() {
      this.cardcoupon = false
      this.isShow = false //关闭知道了弹框
    },
    phone() {
      uni.makePhoneCall({
        phoneNumber: this.serviceNumber.number
      })
    },
    previewImage() {
      uni.previewImage({
        urls: ['https://files.yzsheng.com/client/selfwash/details/map.jpg',
          'https://files.yzsheng.com/client/selfwash/details/map2.jpg',
          'https://files.yzsheng.com/client/selfwash/details/map3.jpg',
          'https://files.yzsheng.com/client/selfwash/details/map4.jpg',
          'https://files.yzsheng.com/client/selfwash/details/map5.jpg'
        ],
        longPressActions: {
          itemList: ['保存图片'],
          success: function(data) {
            uni.saveImageToPhotosAlbum({
              filePath: [
                'https://files.yzsheng.com/client/selfwash/details/map.jpg',
                'https://files.yzsheng.com/client/selfwash/details/map2.jpg',
                'https://files.yzsheng.com/client/selfwash/details/map3.jpg',
                'https://files.yzsheng.com/client/selfwash/details/map4.jpg',
                'https://files.yzsheng.com/client/selfwash/details/map5.jpg',
              ],
              success: function() {
                uni.showToast({
                  title: '图片已保存到本地',
                  icon: 'none',
                })
              },
            })
          },
          fail: function(err) {
            console.log(err.errMsg)
          },
        },
      })
    },
  }
}
